/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.provider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.Text;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.SelectionItem;
import com.xuexiang.xuidemo.slice.refreshlayout.ListSelectionAbilitySlice;

import java.util.ArrayList;
import java.util.List;

/**
 * xinglu
 *
 * @since 2021-04-20
 */
public class SelectionListProvider extends BaseItemProvider {
    private static final int SELECTION_GROUP = 0;
    private static final int SELECTION_CHILD = 1;

    private OnSelectionChangedListener mOnSelectionChangedListener;
    private List<SelectionItem> list;
    private AbilitySlice slice;

    /**
     * 构造方法
     *
     *
     * @param list
     * @param slice
     */
    public SelectionListProvider(List<SelectionItem> list, ListSelectionAbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    /**
     * 监听
     *
     *
     * @param onSelectionChangedListener
     * @return 监听
     */
    public SelectionListProvider setOnSelectionChangedListener(OnSelectionChangedListener onSelectionChangedListener) {
        mOnSelectionChangedListener = onSelectionChangedListener;
        return this;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public SelectionItem getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        SelectionItem item = list.get(position);
        if (getItemComponentType(position) == SELECTION_GROUP) {
            if (convertComponent == null) {
                cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_slice_listselection_title, null, false);
            } else {
                cpt = convertComponent;
            }
            Text mItemTitle = (Text) cpt.findComponentById(ResourceTable.Id_item_title);
            Text mItemTitle1 = (Text) cpt.findComponentById(ResourceTable.Id_item_title1);
            Text mItemTitle2 = (Text) cpt.findComponentById(ResourceTable.Id_item_title2);
            mItemTitle.setText(item.subjectTitle);
            mItemTitle1.setText(item.selectionTitle1);
            mItemTitle2.setText(item.selectionTitle2);
        } else {
            if (convertComponent == null) {
                cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_slice_listselection, null, false);
            } else {
                cpt = convertComponent;
            }
            Text mItemIndex = (Text) cpt.findComponentById(ResourceTable.Id_item_index);
            mItemIndex.setText(item.subjectName);
            RadioContainer mRadioContainer = (RadioContainer) cpt.findComponentById(ResourceTable.Id_radioContainer);
            Component mRadioButton1 = cpt.findComponentById(ResourceTable.Id_radio_button_1);
            Component mRadioButton2 = cpt.findComponentById(ResourceTable.Id_radio_button_2);

            // tag = 0 表示mRadioBUtton1被选中, tag= 1表示mRadioBUtton2被选中
            final int[] tag = {-1};
            mRadioButton1.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (tag[0] == 1) {
                        mRadioButton1.setClickable(true);
                    }
                    tag[0] = 0;
                    mRadioButton1.setClickable(false);
                    mRadioButton2.setClickable(true);
                }
            });

            mRadioButton2.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (tag[0] == 0) {
                        mRadioButton2.setClickable(true);
                    }
                    tag[0] = 1;
                    mRadioButton2.setClickable(false);
                    mRadioButton1.setClickable(true);
                }
            });

            mRadioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
                @Override
                public void onCheckedChanged(RadioContainer radioContainer, int index) {
                    switch (index) {
                        case 0:
                            item.setSelection(0);
                            break;
                        case 1:
                            item.setSelection(1);
                            break;
                        default:
                            break;
                    }
                    if (mOnSelectionChangedListener != null) {
                        mOnSelectionChangedListener.onSelectionChanged(item);
                    }
                }
            });
        }
        return cpt;
    }

    @Override
    public int getItemComponentType(int position) {
        return getItem(position).isTitle() ? SELECTION_GROUP : SELECTION_CHILD;
    }

    /**
     * 获取选择选项的数量
     *
     * @return 选择选项的数量
     */
    public int[] getSelectionCount() {
        int count1 = 0;
        int count2 = 0;
        for (SelectionItem item : list) {
            if (item.isSelection1()) {
                count1++;
            } else if (item.isSelection2()) {
                count2++;
            }
        }
        return new int[]{count1, count2};
    }

    /**
     * 选择结果
     *
     * @return 选择结果
     */
    public List<SelectionItem> getSelectionResult() {
        List<SelectionItem> items = new ArrayList<>();
        for (SelectionItem item : list) {
            if (!item.isTitle()) {
                items.add(item);
            }
        }
        return items;
    }

    /**
     * 选择发生变化的监听
     *
     *  @since 2021-04-20
     */
    public interface OnSelectionChangedListener {
        /**
         * 选择发生变化
         *
         * @param item
         */
        void onSelectionChanged(SelectionItem item);
    }
}
